#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QFile>
#include <QDateTime>
#include <QPdfWriter>
#include <QPainter>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_actionReport_triggered()
{
    std::string filePath = ".";
    std::string fileName = filePath + "\\Report.pdf";
    QString fileNameString = QString::fromStdString(fileName);
    QFile pdfFile(fileNameString);
    pdfFile.open(QIODevice::WriteOnly);
    QPdfWriter *pdfWriter = new QPdfWriter(&pdfFile);
    pdfWriter->setPageSize(QPagedPaintDevice::A4);
    pdfWriter->setResolution(300);//像素3508*2479
    pdfWriter->setTitle("Report");
    int pageMargin = 100;
    pdfWriter->setPageMargins(QMarginsF(pageMargin, pageMargin, pageMargin, pageMargin));
    QDateTime currentTime = QDateTime::currentDateTime();
    QString timeString = currentTime.toString("yyyy-MM-dd hh:mm:ss ddd");
    QPainter *pdfPainter = new QPainter(pdfWriter);
    QTextOption option(Qt::AlignHCenter | Qt::AlignVCenter);
    option.setWrapMode(QTextOption::WordWrap);
    int yCurrentP = 0;
    int xCurrentP = 0;
    int contentWidth = 2479 - pageMargin;
    QFont font;
    font.family();
//    font.setFamily("simsun.ttc");
    font.setFamily("Times New Roman");
    int fontSize = 9;
    int textHeight = 90;
    font.setPointSize(fontSize);
    pdfPainter->setFont(font);
    std::string titleImageFileName = filePath + "\\Imager.png";
    QPixmap titleImage;
    titleImage.load(QString::fromStdString(titleImageFileName));
    //    pdfPainter->drawPixmap(xCurrentP, yCurrentP, titleImage.width(), titleImage.height(), titleImage);
    //    pdfPainter->scale(0.5, 0.5);
    pdfPainter->drawPixmap(xCurrentP, yCurrentP, titleImage.width() / 2, titleImage.height() / 2, titleImage);
    //    pdfPainter->scale(1.5, 1.5);
    option.setAlignment(Qt::AlignRight | Qt::AlignBottom);
    int pageIndex = 1;
    int totalPageIndex = 2;
    QString pageIndexString = QString::number(pageIndex) + "/" + QString::number(totalPageIndex);
    pdfPainter->drawText(QRect(xCurrentP, yCurrentP, contentWidth, titleImage.height() / 2),
                         pageIndexString, option);
    yCurrentP += (titleImage.height() / 2 + 4);
    QPen pen;
    int penHeigth = 4;
    pen.setWidth(penHeigth);
    pen.setColor(Qt::blue);
    pdfPainter->setPen(pen);
    pdfPainter->drawLine(xCurrentP, yCurrentP, contentWidth, yCurrentP);
    pen.setColor(Qt::black);
    pdfPainter->setPen(pen);
    yCurrentP += penHeigth;
    yCurrentP += 100;
    fontSize = 22;
    font.setPointSize(fontSize);
    pdfPainter->setFont(font);
    option.setAlignment(Qt::AlignCenter);
    pdfPainter->drawText(QRect(0, yCurrentP, contentWidth, textHeight),
                         QString("分析报告"), option);
    yCurrentP += (textHeight + 100);
    fontSize = 16;
    font.setPointSize(fontSize);
    pdfPainter->setFont(font);
    option.setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
    pdfPainter->drawText(QRect(xCurrentP, yCurrentP, contentWidth, 80),
                         QString("通道微滴分布图"));
    yCurrentP += (80 + 100);
    xCurrentP += 200;
    option.setWrapMode(QTextOption::WordWrap);
    pdfPainter->drawText(QRect(xCurrentP, yCurrentP, contentWidth - xCurrentP, 400),
                         QString("ROX通道分布图，FAM通道分布图，HEX通道分布图，CY5通道分布图。ROX通道分布图，FAM通道分布图，HEX通道分布图，CY5通道分布图。ROX通道分布图，FAM通道分布图，HEX通道分布图，CY5通道分布图。"),
                         option);
    yCurrentP += (400 + 100);
    std::string imageFileName = filePath + "\\ROX.bmp";
    QPixmap roxImage = QPixmap(QString::fromStdString(imageFileName));
    //    int multipe = rect.width() / roxImage.width();
    //    pdfPainter->scale(multipe, multipe);
    pdfPainter->drawPixmap(0, yCurrentP, roxImage.width() / 2, roxImage.height() / 2, roxImage);
    //    pdfPainter->scale(1, 1);
    std::string famFileName = filePath + "\\FAM.bmp";
    QPixmap famImage = QPixmap(QString::fromStdString(famFileName));
    //    multipe = rect.width()/ famImage.width();
    //    pdfPainter->scale(multipe, multipe);
    pdfPainter->drawPixmap(contentWidth / 2, yCurrentP, famImage.width() / 2, famImage.height() / 2, famImage);
    //    pdfPainter->scale(1, 1);
    yCurrentP += ( std::max(roxImage.height(), famImage.height()) + 100);
    xCurrentP = 0;
    pdfPainter->drawText(QRect(xCurrentP, yCurrentP, contentWidth - xCurrentP, 80),
                         QString("交叉分析结果"), option);
    std::string logo = filePath + "\\Imager.png";
    QPixmap logoImage;
    logoImage.load(QString::fromStdString(logo));
    yCurrentP = 3508 - pageMargin - ( logoImage.height() / 2) + 4;
    pen.setColor(Qt::blue);
    pdfPainter->setPen(pen);
    pdfPainter->drawLine(0, yCurrentP, contentWidth, yCurrentP);
    yCurrentP = 3508 - pageMargin - ( logoImage.height() / 2);
    option.setAlignment(Qt::AlignLeft | Qt::AlignTop);
    fontSize = 9;
    font.setPointSize(fontSize);
    pen.setColor(Qt::black);
    pdfPainter->setPen(pen);
    pdfPainter->drawPixmap(contentWidth - xCurrentP - logoImage.width() / 2, yCurrentP, logoImage.width() / 2, logoImage.height() / 2, logoImage);
    pdfPainter->setFont(font);
    pdfPainter->drawText(QRect(0, yCurrentP, 600, logoImage.height()), timeString, option);
    delete pdfPainter;
    delete pdfWriter;
    pdfFile.close();
}
